package com.nc.delivery_project.server.dao;

import com.nc.delivery_project.server.domain.Currency;
import com.nc.delivery_project.server.domain.Payment;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Natalie D.
 * Date: 1/17/15
 * Time: 10:42 PM
 */
public class PaymentDao extends AbstractDao<Payment>
{

    protected final static String ENTITY_NAME = Payment.class.getSimpleName();


    public PaymentDao() {
        super(ENTITY_NAME);
    }
    public PaymentDao(Connection connection) throws SQLException {
        super(connection, ENTITY_NAME);
    }

    @Override
    protected void prepareInsertStatement(CallableStatement statement, Payment object) throws SQLException
    {
       // statement.setString(1, ""+object.getId());
        statement.registerOutParameter(1, Types.NUMERIC);
        statement.setString(2, object.getIncomingDate());
        statement.setDouble(3, object.getAmount());//
        statement.setInt(4, object.getCurrency().getId());
    }

    @Override
    protected void prepareUpdateStatement(CallableStatement statement, Payment object) throws SQLException {
        statement.setLong(1, object.getId());
        statement.setLong(2, object.getId());
        statement.setString(3, object.getIncomingDate());
        statement.setDouble(4, object.getAmount());
        statement.setInt(5, object.getCurrency().getId());
    }


    @Override
    protected List<Payment> parseResultSet(ResultSet rs) throws SQLException {
        List<Payment> payments = new ArrayList<>();
        while (rs.next())
        {
            int id=rs.getInt("PAYMENT_ID");
            String incomingDate=rs.getString("INCOMING_DATE");
            Double amount=rs.getDouble("AMOUNT");
            Currency currency = new Currency(rs.getInt("CURRENCY"));
            Payment p = new Payment((long)id,incomingDate,currency,amount);
            payments.add(p);
        }

        return payments;
    }
    @Override
    public int create(Payment obj) throws SQLException {
        return super.create(obj, 1);
    }
}

